package sg.edu.nus.iss.ooad.jass.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.dao.IAdvertiserDAO;
import sg.edu.nus.iss.ooad.jass.dao.pattern.AbstractJASSDAOFactory;
import sg.edu.nus.iss.ooad.jass.entity.Advertiser;

/**
 *
 * Advertiser Controller. This class will handle all request related to the Advertiser object.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : AdvertiserController.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class AdvertiserController {

        private final static Logger logger = Logger.getLogger(Advertiser.class.getName());
        private Advertiser advertiser = null;

        /**
         * Advertiser Constructor
         */
        public AdvertiserController() {
        }

        /**
         * This operation will create Advertiser into persistent data.
         * @param advertiser The Advertiser
         * @return True if new Advertiser successfully created otherwise will return false.
         * @throws java.lang.Exception
         */
        public boolean createAdvertiser(Advertiser advertiser) throws Exception {
                IAdvertiserDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.createAdvertiser(advertiser);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will retrieve Advertiser object from persistent.
         * @param id Advertiser ID
         * @return Advertiser object.
         * @throws java.lang.Exception
         */
        public Advertiser retrieveAdvertiser(String id) throws Exception {
                IAdvertiserDAO dao = null;
                Advertiser retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.retrieveAdvertiser(id);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will retrieve Advertiser object from persistent based on user id.
         * @param userId Advertiser UserID
         * @return Advertiser object.
         * @throws java.lang.Exception
         */
        public Advertiser getAdvertiserByUserID(String userID) throws Exception {
                IAdvertiserDAO dao = null;
                Advertiser retValue = null;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.getAdvertiserByUserID(userID);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will update Advertiser object to the persistent.
         * @param advertiser Advertiser object.
         * @return True if Advertiser object successfully updated to the persistent.
         * @throws java.lang.Exception
         */
        public boolean updateAdvertiser(Advertiser advertiser) throws Exception {
                IAdvertiserDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.updateAdvertiser(advertiser);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will update Advertiser object to the persistent.
         * @param advertiser Advertiser object.
         * @return True if Advertiser object successfully updated to the persistent.
         * @throws java.lang.Exception
         */
        public boolean deleteAdvertiser(Advertiser advertiser) throws Exception {
                IAdvertiserDAO dao = null;
                boolean retValue = false;

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.deleteAdvertiser(advertiser);
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;
        }

        /**
         * This operation will return/retrieve all Advertiser objects available from persistent.
         * @return Lists of Advertiser object.
         * @throws java.lang.Exception
         */
        public List<Advertiser> getAllAdvertiser() throws Exception {

                IAdvertiserDAO dao = null;
                List<Advertiser> retValue = new ArrayList<Advertiser>();

                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                        retValue = dao.getAllAdvertiser();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }

                return retValue;

        }

        /**
         * Load Advertiser object from persistent and cache it.
         * @throws java.lang.Exception
         */
        public synchronized static void cache() throws Exception {
                IAdvertiserDAO dao = null;
                try {
                        dao = AbstractJASSDAOFactory.getJASSDAO().getAdvertiserDAO();
                } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                        throw ex;
                } finally {
                        dao = null;
                }
        }
}
